package com.yc.sole.erp.controller;

import com.yc.sole.erp.biz.ProductBiz;
import com.yc.sole.erp.request.ProductQueryReq;
import com.yc.sole.erp.request.ProductReq;
import com.yc.sole.erp.response.ProductResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 产品
 *
 * @author yizuomin
 * @date 2023-12-15
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class ProductController {

    private final ProductBiz productBiz;

    /**
     * 新增
     *
     * @param productReq
     * @return RestResult
     */
    @PreAuth(value = "erp:product:save", description = "新增产品信息")
    @PostMapping("/api/erp/product/saveProduct")
    public Result<Void> saveProduct(@RequestBody @Validated(SaveGroup.class) ProductReq productReq) {
        productBiz.saveProduct(productReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param productReq
     * @return RestResult
     */
    @PreAuth(value = "erp:product:update", description = "修改产品信息")
    @PostMapping("/api/erp/product/updateProductById")
    public Result<Void> updateProductById(@RequestBody @Validated(UpdateGroup.class) ProductReq productReq) {
        productBiz.updateProductById(productReq);
        return Result.success();
    }

    /**
     * 通过id查询
     *
     * @param productId
     * @return RestResult
     */
    @PreAuth(value = "erp:product:select", description = "查询产品信息")
    @PostMapping("/api/erp/product/selectProductById/{productId}")
    public Result<ProductResp> selectProductById(@PathVariable Long productId) {
        return Result.data(productBiz.selectProductById(productId));
    }

    /**
     * 通过名称查询
     *
     * @param productQueryReq
     * @return RestResult
     */
    @PreAuth(value = "erp:product:select", description = "查询产品信息")
    @PostMapping("/api/erp/product/selectProductByName")
    public Result<List<ProductResp>> selectProductByName(@RequestBody ProductQueryReq productQueryReq) {
        return Result.data(productBiz.selectProductByName(productQueryReq.getProductName()));
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "erp:product:delete", description = "删除产品信息")
    @PostMapping("/api/erp/product/deleteProduct")
    public Result<Void> deleteProduct(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        productBiz.deleteProduct(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:product:select", description = "查询产品信息")
    @PostMapping("/api/erp/product/selectProductPage")
    public Result<Paging<ProductResp>> selectProductPage(@RequestBody @Validated BasePageReq<ProductQueryReq> basePageReq) {
        return Result.data(productBiz.selectProductPage(basePageReq.toPage(), basePageReq.getQuery()));
    }
}
